package lsl.leetCode.year2023.test0205;

import lsl.leetCode.test1222.ListNode;

import java.util.ArrayList;

/**
 * 234. 回文链表
 *
 * @author Zed
 * @description
 * @date 2023/2/5 20:36
 */
public class test234 {
    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode hero2 = new ListNode(2);
        ListNode hero3 = new ListNode(2);
        ListNode hero4 = new ListNode(1);
        //创建单链表
        listNode1.next = hero2;
        hero2.next = hero3;
        hero3.next = hero4;
        System.out.println(isPalindrome(listNode1));
    }

    public static boolean isPalindrome(ListNode head) {
        ArrayList<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        int n = list.size();
        int back = n - 1;
        int front = 0;
        for (int i = 0; i < n; i++) {
            if (!list.get(front).equals(list.get(back))) {
                return false;
            }
            front++;
            back--;
        }
        return true;
    }
}
